package com.doget.comment.utils;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Pair;

/**
 * Created by CK on 2018/6/4.
 * Email:910663958@qq.com
 */

public class DisplayUtil {
    /**
     * 获取屏幕宽高
     * @return
     */
    public static Pair getScreenParams(){
        DisplayMetrics dm = new DisplayMetrics();
        int width = dm.widthPixels;
        int heigth = dm.heightPixels;
        return new Pair(width,heigth);
    }

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels+getMiSupplementHeight(context);
    }


    /**
     * 获取需要补充的高度
     * @param context
     * @return
     */
    public static int getMiSupplementHeight(Context context) {
        int result = 0;
        //是否是小米系统，不是小米系统则不需要补充高度
        if(Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
                    && Settings.Global.getInt(context.getContentResolver(), "force_fsg_nav_bar", 0) == 0) {
                //如果虚拟按键已经显示，则不需要补充高度
            } else {
                //如果虚拟按键没有显示，则需要补充虚拟按键高度到屏幕高度
                Resources res = context.getResources();
                int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    result = res.getDimensionPixelSize(resourceId);
                }
            }
        }

        return result;
    }

    public static float getDisplayDensity(Context context) {
        if (context == null) {
            return -1;
        }
        return context.getResources().getDisplayMetrics().density;
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
}
